<?php

namespace app\models\user;

use app\models\constant\Status;
use app\models\marketing\MarketingGroupLevel;
use app\models\merchant\Merchant;
use app\models\order\Order;
use Yii;

/**
 * This is the model class for table "user".
 *
 * @property int $id
 * @property int $referrer_id 推荐人ID
 * @property string $password
 * @property string $mobile
 * @property string $nickname
 * @property string $avatar
 * @property int $level 等级
 * @property string $create_time 注册时间
 * @property string $update_time
 * @property string $login_ip 登录IP
 * @property string $openid 微信openid
 * @property string $unionid 微信unionid
 * @property string $openid_expired 微信openid过期时间
 * @property int $status
 * @property int $reset_level 是否调整成有等级的身份
 * @property int $sex 1 男 2 女 0未获取
 * @property int $admin_ops 1 男 0 女 -1未获取
 * @property int $group_level 团队长v级
 * @property int $card 蜜卡数量
 * @property int $reset_sale 处理复投
 * @property int $is_big_group 是否为大团长
 * @property float $order_amount (品牌区)下单金额
 * @property float $sale_commission 销售(业务)奖金
 * @property int $address_up 指定区域没有
 * @property int $sort 用户排号
 * @property int $store_warn 云仓预警
 * @property int $sell_1
 * @property int $sell_2
 * @property int $sell_3
 * @property int $sell_4
 * @property int $b841
 * @property int $b842
 */
class User extends \yii\db\ActiveRecord
{
    /**
     * status list
     * @var int[]
     */
    public static $statusMapping = [
        'ALL'       => -1,
        'OK'        => 1,
        'CLOSE'     => 0,
        'DELETE'    => -99,
    ];

    /**
     * status display list
     * @var int[]
     */
    public static $statusMappingDisplay = [
        '全部'       => -1,
        '正常'       => 1,
        '拉黑'       => 0,
        // '删除'       => -99,
    ];

    /**
     * Status list
     *
     * @return array
     */
    public static function statusList()
    {
        $list = [];
        foreach (self::$statusMappingDisplay as $key => $value) {
            $list[] = [
                'label' => $key,
                'value' => $value
            ];
        }

        return $list;
    }

    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'user';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['referrer_id', 'level', 'status'], 'integer'],
            [['create_time', 'update_time', 'openid_expired'], 'safe'],
            [['password', 'mobile', 'login_ip', 'openid'], 'string', 'max' => 255],
            // [['mobile'], 'safe'],

            // unionid not use
            [['sex', 'admin_ops', 'unionid'], 'safe'],

            [['group_level'], 'safe'],

            [['card'], 'safe'],

            // province, city level
            [['province_level', 'city_level'], 'safe'],

            // sale
            [['reset_sale', 'order_amount', 'sale_commission'], 'safe'],

            // is big group
            [['is_big_group'], 'safe'],

            // avatar length
            [['avatar', 'nickname'], 'safe'],

            // 指定区域身份
            [['address_up'], 'safe'],

            // 是否为复购了
            [['repeat'], 'safe'],

            // mini
            [['base64Image'], 'safe'],
            [['session_key'], 'safe'],
            [['password', 'mobile'], 'safe'],

            [['sort'], 'safe'],

            // 预警
            [['store_warn'], 'safe'],

            // 业绩奖
            [['sell_1', 'sell_2', 'sell_3', 'sell_4'], 'safe'],

            [['b841', 'b842'], 'safe'],

        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'referrer_id' => 'Referrer ID',
            'password' => 'Password',
            'mobile' => 'Mobile',
            'nickname' => 'Nickname',
            'avatar' => 'Avatar',
            'level' => 'Level',
            'create_time' => 'Create Time',
            'update_time' => 'Update Time',
            'login_ip' => 'Login Ip',
            'openid' => 'Openid',
            'openid_expired' => 'Openid Expired',
            'status' => 'Status',
        ];
    }

    public function beforeSave($insert)
    {
        if (parent::beforeSave($insert)) {
            $time = date('Y-m-d H:i:s');
            $this->update_time = $time;
            if ($insert) {
                $this->create_time = $time;
            }
            return true;
        }
        return false;
    }

    /**
     * with account
     *
     * @return \yii\db\ActiveQuery
     */
    public function getAccount()
    {
        return $this->hasOne(UserAccount::class, ['user_id' => 'id']);
    }

    /**
     * with level
     *
     * @return \yii\db\ActiveQuery
     */
    public function getLevel0()
    {
        return $this->hasOne(UserLevel::class, ['value' => 'level']);
    }

    /**
     * with cart
     *
     * @return \yii\db\ActiveQuery
     */
    public function getCart()
    {
        return $this->hasMany(UserCart::class, ['user_id' => 'id']);
    }

    /**
     * with referrer
     *
     * @return \yii\db\ActiveQuery
     */
    public function getReferrer()
    {
        return $this->hasOne(User::class, ['id' => 'referrer_id'])
            ->select(['id', 'mobile', 'referrer_id', 'nickname']);
    }

    public function getMerchant()
    {
        return $this->hasOne(Merchant::class, ['user_id' => 'id']);
    }

    public function getGroupLevel0()
    {
        return $this->hasOne(MarketingGroupLevel::class, ['id' => 'group_level']);
    }

    public function getOrder()
    {
        return $this->hasMany(Order::class, ['user_id' => 'id'])
            ->where(['!=', 'status', Order::$statusMapping['DELETE']])
            ->andWhere(['!=', 'status', Order::$statusMapping['NON_PAYMENT']])
            ->andWhere(['!=', 'status', Order::$statusMapping['CANCEL']]);
    }
}
